package com.shenbianyun.youfu;

import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.Merchant;
import com.shenbianyun.bosskgpay.utils.*;
import com.shenbianyun.youfu.entity.Payment;
import com.shenbianyun.youfu.entity.RequestMsg;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JmeterTestPaymentYoufu extends AbstractJavaSamplerClient {


        private String merchant;
        private String PriKey;
        private String InterKey;
        private String url;
        private  String charset = "UTF-8";
        private String levy_id;
        /** Holds the result data (shown as Response Data in the Tree display). */
        private boolean resultData;
        static Merchant mer = null;

        // 这个方法是用来自定义java方法入参的。
        // params.addArgument("num1","");表示入参名字叫num1，默认值为空。
        //设置可用参数及的默认值；
        public Arguments getDefaultParameters() {
            Arguments params = new Arguments();
            params.addArgument("merchant", "89900000040415665332");//设置参数，并赋予默认值1
            params.addArgument("PriKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXDDAmxWoqm5Z/9JB6Ivz0tRVRQ3cMn+LVcYGnVZFKcZPou4ZnjzubTLU4/4njbKKrYmhFx1oZCvc+V/2D79XY0f/1L09D+rTGPGCTquEJ1k66qf+VnFUW40WUOJa7WQR7ta1/fFOWn6+J/shqQj6AmA0442eiWcjDvSw8zBT9zAgMBAAECgYBBoPhYDR+KZebGP7KoFJoYDw00GFUmOodpnIBljOCjM77LjZL9KWvLjxNskCWy/HyQjqPRKVSDHV356vOZByZgcvpoobEh0hM2OBc49J6Ne46qRwe2N8ugzcfFml1pmxWqf8kyaer+KMBBv3VyxIRllwYW/9RphbYPwAEhc6Xo8QJBAPVOPulpoVP7nrDEMla9D650bZxcUzL6EGgRhvMbDeVAaxVowMFyRgdzXCXYpNVsnqj4+f1NvzQTfRbzQkmRBxsCQQCLl+hvmc6z6ajhIoG1w/oditSTOvnLMRLUyQtJI1ttGHJjo+03b14AtJDk6Qniuke8n3hvFB/QSfGBgi75hnaJAkAvbXxuZpThhvGsnZJaAN3KTgpvQ852ObD4XgMod5+dJnJNxl4WSMDPafdz/coI1aJraw/ycJi6OdZinI/BZ3/nAkAAu5NkwpHrL9bLF7tGkPjHHutuumiFQISwyT1d7OmV8QLosvuTxunfZxM7oF2E6yVlQBEJxDlGhpzV6TEF7kbBAkAbz8tXcz08OonqYxF6ro2cnE9KpmF1W4nhQcqZZ6L7lbEZJ2+tjYuPGCiGILU11F8KxtgtzwZ+h8RAUilFKTC4");//设置参数，并赋予默认值2
            params.addArgument("InterKey", "540E40DFD64376E1B1D2CD53D236F200");//设置参数，并赋予默认值2
            params.addArgument("url", "http://api.youfupingtai.com/pctrans-business/clientBusiness/common.do");//设置参数，并赋予默认值2
            params.addArgument("levy_id", "32719");//设置参数，并赋予默认值2
            params.addArgument("ProjectId", "1610278112283253314");//设置参数，并赋予默认值2

            return params;
        }


        //开始测试，从arg0参数可以获得参数值；
        public SampleResult runTest(JavaSamplerContext arg0) {
            SampleResult sr = new SampleResult();
            try {


            sr.sampleStart();// jmeter 开始统计响应时间标记

            merchant =arg0.getParameter("merchant");
            PriKey = arg0.getParameter("PriKey");
            InterKey = arg0.getParameter("InterKey");
            url = arg0.getParameter("url");
            levy_id = arg0.getParameter("levy_id");
            //sr.setSampleLabel( "Java请求哦");

            RequestMsg rm = new RequestMsg();
            rm.setFunCode("6001");
            rm.setMerId(merchant);
            rm.setVersion("V1.0");
            Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
            rm.setReqId(id+"");

            String json = getJson(arg0);
            System.out.println("明文请求体"+json);
            byte[] bs = new byte[0];
            try {
                bs = DESUtils.encrypt(json.getBytes(charset), InterKey);
            } catch (Exception e) {

                e.printStackTrace();
            }
            String reqDataEncrypt = Base64Utils.encodeToString(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, PriKey));
            String reqStr = JsonUtils.toJson(rm);
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("reqJson", reqStr);
                System.out.println("请求加密数据"+reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, charset);
            System.out.println(resData);
                JSONObject jsonObject1 = JSONObject.parseObject(resData);
                // 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
                if (resultData = jsonObject1.get("resCode").equals("0000")) {
                    sr.setResponseData("结果是："+resultData, null);

                    sr.setDataType(SampleResult.TEXT);
                    System.out.println("结果："+resultData);
                }
                // System.out.println(resultData);
                sr.setSuccessful(true);
            } catch (Throwable e) {
                System.out.println("异常了");
                sr.setSuccessful(false);
                e.printStackTrace();
            } finally {
                sr.sampleEnd();// jmeter 结束统计响应时间标记
            }
            return sr;
        }

        public static String getJson(JavaSamplerContext arg0){

        /**--------————————————业务请求参数——————————————————**/
        Payment payment = new Payment();
        //商户号
        payment.setMerId(arg0.getParameter("merchant"));
        //项目Id
        payment.setProjectId(arg0.getParameter("ProjectId"));
        //付款总笔数  这里只模拟一条
        payment.setTotalCount("1");
        //付款总金额 单位/分
        payment.setTotalAmt("2");

        //生成guid码 理论上不重复
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();


        payment.setMerBatchId(id+"");
        //payment.setMerBatchId("BatchPay000002");
        //批量付款接口，项目编码
        List payItemsList = new ArrayList();

        Payment.PayItems payItems = new Payment.PayItems();
        //同批次唯一，商户生成

        payItems.setMerOrderId("OrderPay" + id);
        //.setMerOrderId("OrderPay000001");
        /**
         * 必填
         */
        payItems.setAmt(1L);
        payItems.setPayeeName("崔方方");
        payItems.setIdCard("412828199310044519");
        payItems.setMobile("18001150364");
        payItems.setPayeeAcc("6214830102464327");
        // 0 指银行卡
        payItems.setPaymentType(Integer.parseInt("0"));
        // 0 代付类型为实时
        payItems.setPayType(0);
        //税源地ID
        payItems.setLevyId(Long.parseLong(arg0.getParameter("levy_id")));
        //1：对私  0：对公（不支持）
        payItems.setAccType(Integer.parseInt("1"));
        /**
         * 选填
         */
        payItems.setBranchName("北京");
        payItems.setProvince("北京");
        payItems.setCity("北京");
        payItems.setBranchNo("北京");
        payItems.setMemo("服务费贷");

        /**--------————————————去付款——————————————————————**/
        payItemsList.add(payItems);
        payment.setPayItems(payItemsList);
        String json= JsonUtils.toJson(payment);
        return json;
    }

        // main只是为了调试用，最后打jar包的时候注释掉。
        public static void main(String[] args) {
            // TODO Auto-generated method stub

           // for(int i = 0;i<100;i++){

            Arguments params = new Arguments();
            params.addArgument("merchant", "89900000040415665332");//设置参数，并赋予默认值1
            params.addArgument("PriKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXDDAmxWoqm5Z/9JB6Ivz0tRVRQ3cMn+LVcYGnVZFKcZPou4ZnjzubTLU4/4njbKKrYmhFx1oZCvc+V/2D79XY0f/1L09D+rTGPGCTquEJ1k66qf+VnFUW40WUOJa7WQR7ta1/fFOWn6+J/shqQj6AmA0442eiWcjDvSw8zBT9zAgMBAAECgYBBoPhYDR+KZebGP7KoFJoYDw00GFUmOodpnIBljOCjM77LjZL9KWvLjxNskCWy/HyQjqPRKVSDHV356vOZByZgcvpoobEh0hM2OBc49J6Ne46qRwe2N8ugzcfFml1pmxWqf8kyaer+KMBBv3VyxIRllwYW/9RphbYPwAEhc6Xo8QJBAPVOPulpoVP7nrDEMla9D650bZxcUzL6EGgRhvMbDeVAaxVowMFyRgdzXCXYpNVsnqj4+f1NvzQTfRbzQkmRBxsCQQCLl+hvmc6z6ajhIoG1w/oditSTOvnLMRLUyQtJI1ttGHJjo+03b14AtJDk6Qniuke8n3hvFB/QSfGBgi75hnaJAkAvbXxuZpThhvGsnZJaAN3KTgpvQ852ObD4XgMod5+dJnJNxl4WSMDPafdz/coI1aJraw/ycJi6OdZinI/BZ3/nAkAAu5NkwpHrL9bLF7tGkPjHHutuumiFQISwyT1d7OmV8QLosvuTxunfZxM7oF2E6yVlQBEJxDlGhpzV6TEF7kbBAkAbz8tXcz08OonqYxF6ro2cnE9KpmF1W4nhQcqZZ6L7lbEZJ2+tjYuPGCiGILU11F8KxtgtzwZ+h8RAUilFKTC4");//设置参数，并赋予默认值2
            params.addArgument("InterKey", "540E40DFD64376E1B1D2CD53D236F200");//设置参数，并赋予默认值2
            params.addArgument("url", "http://api.youfupingtai.com/pctrans-business/clientBusiness/common.do");//设置参数，并赋予默认值2
            params.addArgument("levy_id", "32719");//设置参数，并赋予默认值2

            JavaSamplerContext arg0 = new JavaSamplerContext(params);
            JmeterTestPaymentYoufu test = new JmeterTestPaymentYoufu();
            test.setupTest(arg0);
            test.runTest(arg0);
            test.teardownTest(arg0);

            //}
        }
    }

